package com.yocard.yxyd.http;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yocard.yxyd.http.model.base.Response;
import com.yocard.yxyd.http.model.request.CertRequest;
import com.yocard.yxyd.http.model.request.CsmListRequest;
import com.yocard.yxyd.http.model.request.OrderListRequest;
import com.yocard.yxyd.http.model.request.PushSettingRequest;
import com.yocard.yxyd.http.model.response.BannerBean;
import com.yocard.yxyd.http.model.response.CertStatusBean;
import com.yocard.yxyd.http.model.response.CoinsBean;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.http.model.response.CsmOrderDetailBean;
import com.yocard.yxyd.http.model.response.CsmOrderListBean;
import com.yocard.yxyd.http.model.response.CsmPayInfo;
import com.yocard.yxyd.http.model.response.InviteRecord;
import com.yocard.yxyd.http.model.response.InviteResponse;
import com.yocard.yxyd.http.model.response.OrderListBean;
import com.yocard.yxyd.http.model.response.OrderPayResponseBean;
import com.yocard.yxyd.http.model.response.PushSettingBean;
import com.yocard.yxyd.http.model.response.RechargesBean;
import com.yocard.yxyd.http.model.response.TopOrderBean;
import com.yocard.yxyd.http.model.response.TrackListBean;
import com.yocard.yxyd.http.model.response.UpdateBean;
import com.yocard.yxyd.http.model.response.UserInfoBean;

import org.json.JSONArray;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created on 2018/9/24.
 */
public class HttpManager {
    private static HttpManager httpManager;
    protected Gson mGson;
    protected Api mApi;

    //    public Flowable<Response<>>

    public Flowable<Response<InviteRecord>> getInviteList(int currentPage, String status) {
        return mApi.getInviteList(currentPage, status);
    }

    public Flowable<Response<InviteResponse>> getInviteInfo() {
        return mApi.getInviteInfo();
    }

    public Flowable<Response<String>> savePushParameters(PushSettingRequest request) {
        JsonObject jsonObject = mGson.toJsonTree(request).getAsJsonObject();
        return mApi.savePushParameters(jsonObject);
    }

    public Flowable<Response<PushSettingBean>> getPushParameters() {
        return mApi.getPushParameters();
    }

    public Flowable<Response<BannerBean>> getBanners() {
        return mApi.getBanners();
    }

    public Flowable<Response<CommonBean>> getRealName() {
        return mApi.getRealName();
    }

    public Flowable<Response<JSONObject>> addAvatorId(String id) {
        return mApi.addAvatorId(id);
    }

    public Flowable<Response<CommonBean>> uploadAvator(File file) {
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        RequestBody typeBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), "customers");
        RequestBody typeBody2 = RequestBody.create(MediaType.parse("multipart/form-data"), "Customer");
        return mApi.uploadImg(typeBody1, typeBody2, body);
    }

    public Flowable<Response<UpdateBean>> getUpdateInfo(int versionCode) {
        return mApi.getUpdateInfo(versionCode,"android");
    }

    public Flowable<Response<List<CoinsBean>>> getCoinsTrans(int page, String type) {
        return mApi.getCoinsTrans(page, type);
    }

    public Flowable<Response<List<Object>>> changeMobile(String mobile, String code) {
        JsonObject jsonObject = ParamsBuilder.create()
                .append("mobile", mobile)
                .append("code", code).build();
        return mApi.changeMobile(jsonObject);
    }

    public Flowable<Response<JsonObject>> getNewMObileCode(String mobile) {
        return mApi.getNewMobileCode(ParamsBuilder.create().append("mobile", mobile).build());
    }

    public Flowable<Response<List<Object>>> pushOldcode(String code) {
        JsonObject jsonObject = ParamsBuilder.create().append("code", code).build();
        return mApi.pushOldcode(jsonObject);
    }

    public Flowable<Response<List<Object>>> getOldMObileCode() {
        return mApi.getOldMObileCode(ParamsBuilder.create().build());
    }

    public Flowable<Response<CertStatusBean>> getCertStatus() {
        return mApi.getCertStatus();
    }

    public Flowable<Response<List<Object>>> postRealName(String name, String idCard, String cityCode, String reCertId) {
        JsonObject jsonObject = ParamsBuilder.create().append("name", name)
                .append("id_card_no", idCard)
                .append("city_code", cityCode)
                .append("id", reCertId)
                .build();
        return mApi.postRealName(jsonObject);
    }

    public Flowable<Response<List<Object>>> cert(CertRequest request) {
        JsonObject jsonObject = mGson.toJsonTree(request).getAsJsonObject();
        return mApi.postRealName(jsonObject);
    }

    public Flowable<Response<Map<String, String>>> getTrackStatus() {
        return mApi.getTrachStatus();
    }

    public Flowable<Response<List<Object>>> commitTrack(String id, String type, String remark) {
        JsonObject jsonObject = ParamsBuilder.create().append("type", type).append("remark", remark).build();
        return mApi.commitTrack(id, jsonObject);
    }

    public Flowable<Response<List<Object>>> commitRefund(String id, String mark, List<String> urls) {
        JsonObject jsonObject = ParamsBuilder.create().append("reason", mark).build();
        jsonObject.add("pic_id", mGson.toJsonTree(urls));
        return mApi.commitRefund(id, jsonObject);
    }

    public Flowable<Response<TrackListBean>> getTrackList(String id) {
        return mApi.getTrackList(id);
    }

    public Flowable<Response<CommonBean>> uploadImg(String type, File file, String attach_type) {
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        RequestBody typeBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), type);
        RequestBody typeBody2 = RequestBody.create(MediaType.parse("multipart/form-data"), attach_type);
        return mApi.uploadImg(typeBody1, typeBody2, body);
    }

    public Flowable<Response<CsmOrderDetailBean>> getCsmDetail(String id) {
        return mApi.getCsmDetail(id);
    }

    public Flowable<Response<CsmOrderListBean>> getCsmOrderList(int currentPage, CsmListRequest request) {
        JsonObject jsonObject = mGson.toJsonTree(request).getAsJsonObject();
        return mApi.getCsmList(currentPage, jsonObject);
    }

    public Flowable<Response<OrderPayResponseBean>> doPay(String orderId) {
        return mApi.doPay(orderId);
    }

    public Flowable<Response<CsmPayInfo>> getCsmOrderInfo(String orderId) {
        return mApi.getCsmOrderInfo(orderId);
    }

    public Flowable<Response<CommonBean>> doOrder(String id) {
        return mApi.doOrder(id);
    }

    public Flowable<Response<UserInfoBean>> getUswrInfo() {
        return mApi.getUserInfo();
    }

    public Flowable<Response<OrderListBean.OrderList>> getOrderDetail(String id) {
        return mApi.getOrderDetail(id);
    }

    public Flowable<Response<OrderListBean>> getOrderList(int page, OrderListRequest request) {
        JsonObject jsonObject = mGson.toJsonTree(request).getAsJsonObject();
        return mApi.getOrderList(page, jsonObject);
    }

    public Flowable<Response<TopOrderBean>> createOrder(String id) {
        JsonObject jsonObject = ParamsBuilder.create().append("recharge_id", id)
                .append("payway", "native")
                .build();
        return mApi.createOrder(jsonObject);
    }

    public Flowable<Response<List<RechargesBean>>> getRecharges() {
        return mApi.getRecharges();
    }

    public Flowable<Response<CommonBean>> loginUser(String mobile, String code, String longitude, String latitude, String device, String push_token) {
        JsonObject jsonObject = ParamsBuilder.create().append("mobile", mobile)
                .append("code", code)
                .append("latitude", latitude)
                .append("longitude", longitude)
                .append("device", device)
                .append("push_token", push_token)
                .append("os", "android")
                .build();
        return mApi.loginUser(jsonObject);
    }

    public Flowable<Response<CommonBean>> refreshToken() {
        return mApi.refreshToken();
    }

    public Flowable<Response<List<Object>>> getSmsCode(String phone) {
        JsonObject jsonObject = ParamsBuilder.create().append("mobile", phone).build();
        return mApi.getSmscode(jsonObject);
    }

    private HttpManager() {
        mGson = new Gson();
        mApi = RetrofitHelper.api();
    }

    public static HttpManager getInstance() {
        if (httpManager == null) {
            synchronized (HttpManager.class) {
                if (httpManager == null) {
                    httpManager = new HttpManager();
                }
            }
        }
        return httpManager;
    }

    static class ParamsBuilder {
        private JsonObject jsonObject;

        private ParamsBuilder() {
            jsonObject = new JsonObject();
        }

        public static ParamsBuilder create() {
            return new ParamsBuilder();
        }

        public ParamsBuilder append(String key, int value) {
            jsonObject.addProperty(key, value);
            return this;
        }

        public ParamsBuilder append(String key, Long value) {
            jsonObject.addProperty(key, value);
            return this;
        }

        public ParamsBuilder append(String key, String value) {
            jsonObject.addProperty(key, value);
            return this;
        }

        public ParamsBuilder append(String key, Boolean value) {
            jsonObject.addProperty(key, value);
            return this;
        }

        public JsonObject build() {
            return jsonObject;
        }

        public String toString() {
            return jsonObject.toString();
        }
    }
}
